<template>
  <div>
    <router-link to="/">
     
    </router-link>
    
    <el-alert v-if="error" type="error" show-icon>{{ error.message }}</el-alert>
    <el-skeleton v-if="loading" :rows="6" animated />
    <el-row v-else :gutter="20">
      <el-col 
        v-for="product in products"
        :key="product.productId?.toString()"
        :xs="24"
        :sm="12"
        :md="8"
        :lg="6"
        class="product-item"
        style="min-width: 280px;"
      >
        <el-card 
          :body-style="{ padding: '10px' }"
          @click="$router.push(`/product/${product.productId?.toString()}`)"
          style="cursor: pointer;"
        >
          <img 
            v-if="product.product_cover_image"
            :src="apiBase + '/product_images/' + product.product_cover_image"
            class="product-image"
            alt="商品封面"
          />
          <img
            v-else
            src="/placeholder-image.jpg"
            class="product-image"
            alt="暂无图片"
          />
          <div class="product-info">
            <h3 class="product-name">{{ product.productName }}</h3>
            <p class="product-price">¥{{ product.productPrice.toFixed(2) }}</p>
          </div>
        </el-card>
      </el-col>
    </el-row>


  </div>
</template>

<script setup lang="ts">
import { ref, watch } from 'vue'
import { useRoute } from 'vue-router'
import axios from 'axios'
import { getToken } from '@/utils/storage.js'

interface Product {
  productId: string
  productName: string
  productPrice: number
  product_cover_image?: string
}

const products = ref<Array<Product>>([])
const loading = ref(true)
const error = ref<Error | null>(null)
const route = useRoute()
const apiBase = ref(import.meta.env.VITE_API_BASE || '')
const keyword = route.query.keyword as string
const categoryId = route.query.category_id as string

const fetchProducts = async () => {
  try {
    loading.value = true;
    
    const token = getToken();
    
    // 新增店铺商品请求处理
    if (route.query.shopId) {
      const response = await axios.get(
        `${apiBase.value}/product/seller/${route.query.shopId}`, 
        {
          headers: { Authorization: `Bearer ${token}` }
        }
      );
      products.value = response.data;
      return;
    }
    
    let url = `${import.meta.env.VITE_API_BASE}/product`;
    
    if (route.query.seller) {
      url = `${import.meta.env.VITE_API_BASE}/product/seller`;
    } else if (keyword) {
      url = `${import.meta.env.VITE_API_BASE}/product/keyword/${encodeURIComponent(keyword)}`;
    } else if (categoryId) {
      url = `${import.meta.env.VITE_API_BASE}/product/category/${categoryId}`;
    }

    const response = await axios.get(url, {
  headers: { Authorization: `Bearer  ${token}` }
});
    products.value = response.data.filter((p: Product) => p != null && p.productId != null);
    } catch (err) {
    error.value = err as Error;
    console.error('获取商品失败:', err);
  } finally {
    loading.value = false;
  }
};

watch(() => [route.query.keyword, route.query.category_id], () => {
  fetchProducts();
});

fetchProducts();
</script>




<style scoped>
.product-container {
  max-width: 1200px;
  margin: 20px auto;
  padding: 20px;
}

.product-list {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 24px;
  padding: 20px 0;
}

.product-card {
  border-radius: 8px;
  overflow: hidden;
  background: #fff;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  transition: all 0.3s ease;
  cursor: pointer;
}

.product-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.15);
}

.product-image {
  width: 100%;
  height: 200px;
  min-height: 180px;
  object-fit: contain;
  border-bottom: 1px solid var(--el-border-color-light);
  padding: 8px;
  background: #f5f5f5;
}

.product-info {
  position: relative;
  padding: 16px;
  min-height: 120px;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}

.product-name {
  font-size: 16px;
  color: var(--el-text-color-primary);
  margin-bottom: 8px;
  line-height: 1.5;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.price-section {
  display: flex;
  align-items: center;
  margin-top: 12px;
}

.price-label {
  font-size: 12px;
  color: var(--el-text-color-secondary);
  margin-right: 4px;
}

.price-value {
  font-size: 18px;
  color: var(--el-color-primary);
  font-weight: 600;
}
.product-price {
  color: #ff0000;
  font-weight: bold;
  margin-top: 8px;
}
</style>



